package net.vkits.controller;

import net.vkits.anjiamiao.application.ContractApplicationService;
import net.vkits.anjiamiao.application.CustomerApplicationService;
import net.vkits.anjiamiao.domain.model.Address;
import net.vkits.anjiamiao.domain.model.Contract;
import net.vkits.anjiamiao.domain.model.Customer;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Created by ethan-liu on 2016/12/18.
 */
@Controller
@RequestMapping("/contracts")
public class ContractController {
    private static final Logger logger = LoggerFactory.getLogger(ContractController.class);

    @Resource
    ContractApplicationService contractApplicationService;
    @Resource
    CustomerApplicationService customerApplicationService;

    @RequiresPermissions("contracts:view")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView contracts() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "contracts");
        modelMap.put("sidebar", "contracts");
        modelMap.put("currentUser",username);
        return new ModelAndView("/contracts", modelMap);
    }

    @RequiresPermissions("contracts:view")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Contract> getContracts(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Contract> contracts = contractApplicationService.getContracts(pageable);
        logger.info("size is: {}", contracts.getTotalElements());
        return contracts;
    }

    @RequiresPermissions("contracts:modify")
    @RequestMapping(value ="/{id}", method = RequestMethod.GET)
    public ModelAndView contract(@PathVariable String id) {
        logger.info("id: {}", id);
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        Contract contract = contractApplicationService.getContractById(id);
        Optional<Customer> customerOptional = contractApplicationService.getCustomerByContractId(id);
        ModelMap modelMap = new ModelMap();
        customerOptional.ifPresent(customer -> modelMap.put("customer", customer));
        modelMap.put("contract", contract);
        modelMap.put("treeview", "contracts");
        modelMap.put("sidebar", "contracts");
        modelMap.put("currentUser", username);
        return new ModelAndView("/contract", modelMap);
    }

    @RequiresPermissions("contracts:modify")
    @RequestMapping(value ="/update", method = RequestMethod.POST)
    public String contractUpdate(@RequestParam(value = "contractId", required = true) String contractId,
                                 @RequestParam(value = "province", required = false) String province,
                                 @RequestParam(value = "city", required = false) String city,
                                 @RequestParam(value = "area", required = false) String area,
                                 @RequestParam(value = "town", required = false) String town,
                                 @RequestParam(value = "addressInfo", required = false) String addressInfo,
                                 @RequestParam(value = "startDate", required = true) String startDate,
                                 @RequestParam(value = "endDate", required = true) String endDate,
                                 @RequestParam(value = "status", required = true) String status) {
        Address address = new Address();
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        address.setStreet(town);
        address.setAddressInfo(addressInfo);
        contractApplicationService.updateContract(contractId,address,startDate,endDate,status);
        return "redirect:/contracts";
    }
}
